/* 
 * Copyright (c) 2008 Knightsoft
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.knightsoft.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Run an operating system command
 * and send the output to the console.
 * 
 * @version V1.0 Nov 08, 2008
 * @author Michael
 * 
 * @exception OSShellException
 * @exception RuntimeException
 */
public class OSShell {
	public static void command(String command) {
		boolean err = false;
		try {
			Process process = new ProcessBuilder(command.split(" ")).start();
			BufferedReader results = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			String s;
			while ((s = results.readLine()) != null)
				System.out.println(s);
			BufferedReader errors = new BufferedReader(new InputStreamReader(
					process.getErrorStream()));
			// Report errors and return nonzero value
			// to calling process if there are problems:
			while ((s = errors.readLine()) != null) {
				System.err.println(s);
				err = true;
			}
		} catch (Exception e) {
			// Compensate for Windows 2000, which throws an
			// exception for the default command line:
			if (!command.startsWith("CMD /C"))
				command("CMD /C " + command);
			else
				throw new RuntimeException(e);
		}
		if (err)
			throw new OSShellException("Errors executing " + command);
	}
	
	//Test
	public static void main(String[] args) {
		OSShell.command("ipconfig -all");
		/* on Linux
		OSExecute.command("ifconfig");
		*/
	}
}

/* Windows Output: 
Windows IP Configuration

        Host Name . . . . . . . . . . . . : h4ck3r

        Primary Dns Suffix  . . . . . . . : 

        Node Type . . . . . . . . . . . . : Unknown

        IP Routing Enabled. . . . . . . . : No

        WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . : 

        Description . . . . . . . . . . . : Intel(R) PRO/100 VE Network Connection

        Physical Address. . . . . . . . . : 00-16-76-50-32-58

        Dhcp Enabled. . . . . . . . . . . : No

        IP Address. . . . . . . . . . . . : 192.168.0.23

        Subnet Mask . . . . . . . . . . . : 255.255.255.0

        Default Gateway . . . . . . . . . : 192.168.0.1

        DNS Servers . . . . . . . . . . . : 61.177.7.1

Ethernet adapter Bluetooth Network Connection:

        Media State . . . . . . . . . . . : Media disconnected

        Description . . . . . . . . . . . : Bluetooth Device (Personal Area Network)

        Physical Address. . . . . . . . . : 00-11-B1-07-A2-47

 */
